<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="js/jquery-1.11.3.min.js"></script>
    <script src="js/md5.js"></script>
    <title>webSocket</title>
</head>
<body>
    <h1>Chat Room</h1>
    <input id="sendTxt" type="text"/>
    <button id="sendBtn">发送</button>
    <div id="recv"></div>
    <script>
        function showMassage(data){
            var div =document.createElement('div')
            div.innerHTML=data;
            document.body.appendChild(div);
        }
        
        function tansful(word){
            var appid = '20151211000007653';
            var key = 'IFJB6jBORFuMmVGDRude';
            var salt = (new Date).getTime();
            var str1 = appid + word + salt + key;
            var sign = MD5(str1);
            $.ajax({
                url: 'http://api.fanyi.baidu.com/api/trans/vip/translate',
                method: 'GET',
                dataType: 'jsonp',  // 请求方式为jsonp
                jsonpCallback: "onBack",    // 自定义回调函数名
                data: {
                    q: word,
                    appid: appid,
                    salt: salt,
                    from: 'auto',
                    to: 'zh',
                    sign: sign
                },
                asyne: true
            }).done(function (data) {
                let transData=data.trans_result;
                for(let p in transData){
                    console.log('transData'+p, transData[p]);
                }
            });
        }

        function onBack(data){
            console.log(data);
        }
        document.getElementById("sendBtn").onclick=function(){
                var txt=document.getElementById("sendTxt").value;
                if(txt){
                    tansful(txt);
                }
            }
    </script>
</body>
</html>